<template>
  <b-container fluid>
    <page-title />
    <b-card no-body>
      <b-tabs pills card>
        <b-tab :title="$t('pagePerformanceMonitor.currentStatus')" active>
          <b-row>
            <b-col :cols="6">
              <page-section
                v-if="hostStatus.hasOwnProperty('CPU')"
                section-title="CPU"
              >
                <b-row class="justify-content-center border border-white">
                  <b-col :cols="6">
                    <performance-monitor-panel
                      ref="CPU"
                      :comp-name="`CPU`"
                      :current-data="
                        hostStatus.CPU.CPU[hostStatus.CPU.CPU.length - 1]
                      "
                    />
                  </b-col>
                </b-row>
              </page-section>
            </b-col>
            <b-col>
              <page-section
                v-if="hostStatus.hasOwnProperty('Memory')"
                :section-title="$t('pagePerformanceMonitor.Memory')"
              >
                <b-row class="justify-content-center border border-white">
                  <b-col :cols="6">
                    <performance-monitor-panel
                      ref="Memory"
                      comp-name="Memory"
                      :current-data="
                        hostStatus.Memory.Memory[
                          hostStatus.Memory.Memory.length - 1
                        ]
                      "
                    />
                  </b-col>
                </b-row>
              </page-section>
            </b-col>
          </b-row>

          <template v-for="(historyData, hardwareName) in hostStatus">
            <page-section
              v-if="hardwareName == 'Disk'"
              :key="hardwareName"
              :section-title="$t('pagePerformanceMonitor.Disk')"
            >
              <b-row>
                <b-col
                  v-for="(allData, compName) in historyData"
                  :key="compName"
                  class="border border-white"
                  cols="3"
                >
                  <performance-monitor-panel
                    :ref="hardwareName"
                    :comp-name="compName"
                    :current-data="allData[allData.length - 1]"
                  />
                </b-col>
              </b-row>
            </page-section>
            <page-section
              v-if="hardwareName === 'NetInterface'"
              :key="hardwareName"
              :section-title="$t('pagePerformanceMonitor.NetInterface')"
            >
              <b-row>
                <b-col :cols="3">{{ $t('pagePerformanceMonitor.NIC') }}</b-col>
                <b-col :cols="3">{{
                  $t('pagePerformanceMonitor.downSpeed')
                }}</b-col>
                <b-col :cols="3">{{
                  $t('pagePerformanceMonitor.upSpeed')
                }}</b-col>
                <b-col :cols="3">{{
                  $t('pagePerformanceMonitor.maxSpeed')
                }}</b-col>
              </b-row>
              <b-row
                v-for="(compData, compName) in hostStatus.NetInterface"
                :key="compName"
              >
                <b-col :cols="3">{{ compName }}</b-col>
                <b-col :cols="3">{{
                  compData[compData.length - 1].downSpeed +
                  compData[compData.length - 1].unit
                }}</b-col>
                <b-col :cols="3">{{
                  compData[compData.length - 1].upSpeed +
                  compData[compData.length - 1].unit
                }}</b-col>
                <b-col :cols="3">{{
                  compData[compData.length - 1].maxSpeed +
                  compData[compData.length - 1].unit
                }}</b-col>
              </b-row>
            </page-section>
          </template>
        </b-tab>
        <b-tab :title="$t('pagePerformanceMonitor.history')">
          <b-row>
            <b-col md="5" lg="4" xl="3">
              <b-form-select
                v-model="selected"
                :options="options"
              ></b-form-select>
            </b-col>
          </b-row>
          <b-row>
            <b-col
              v-for="(historyData, hardwareName) in {
                CPU: hostStatus.CPU,
                Memory: hostStatus.Memory,
              }"
              :key="hardwareName"
              sm="12"
              md="12"
              lg="6"
              xl="6"
            >
              <performance-monitor-history-chart
                :hardware-name="hardwareName"
                :history-data="historyData[hardwareName]"
                :selected="selected"
                :hardware-type="hardwareName"
              />
            </b-col>
          </b-row>
          <b-row
            v-for="(historyData, hardwareName) in {
              Disk: hostStatus.Disk,
              NetInterface: hostStatus.NetInterface,
            }"
            :key="hardwareName"
          >
            <b-col :cols="12">
              <div class="text-center font-weight-bold">
                {{ historyBlockName(hardwareName) }}
              </div>
            </b-col>
            <b-col
              v-for="(allData, compName) in historyData"
              :key="compName"
              sm="12"
              md="12"
              lg="6"
              xl="6"
            >
              <performance-monitor-history-chart
                :hardware-name="compName"
                :history-data="allData"
                :selected="selected"
                :hardware-type="hardwareName"
              />
            </b-col>
          </b-row>
        </b-tab>
      </b-tabs>
    </b-card>
  </b-container>
</template>

<script lang="ts">
import PageTitle from '@/components/Global/PageTitle';
import PageSection from '@/components/Global/PageSection.vue';
import PerformanceMonitorPanel from './PerformanceMonitorPanel';
import PerformanceMonitorHistoryChart from './PerformanceMonitorHistoryChart';
import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';

export default {
  name: 'PerformanceMonitor',
  components: {
    PageTitle,
    PageSection,
    PerformanceMonitorPanel,
    PerformanceMonitorHistoryChart,
  },
  mixins: [LoadingBarMixin],
  data() {
    return {
      timer: '',
      selected: '15 minutes',
      options: [
        {
          value: '48 hours',
          text: this.$t('pagePerformanceMonitor.options.h48'),
        },
        {
          value: '24 hours',
          text: this.$t('pagePerformanceMonitor.options.h24'),
        },
        {
          value: '12 hours',
          text: this.$t('pagePerformanceMonitor.options.h12'),
        },
        { value: '1 hour', text: this.$t('pagePerformanceMonitor.options.h1') },
        {
          value: '15 minutes',
          text: this.$t('pagePerformanceMonitor.options.m15'),
        },
      ],
    };
  },
  computed: {
    hostStatus: {
      get() {
        const tmp = this.$store.getters['performanceMonitor/hostStatus'] as {
          [index: string]: any;
        };
        delete tmp.SysInfo;
        return tmp;
      },
    },
  },
  mounted() {
    this.startLoader();
    this.$store
      .dispatch('performanceMonitor/getHostStatus')
      .catch(() => {
        clearInterval(this.timer);
        this.$bvModal.msgBoxOk(
          this.$t('pagePerformanceMonitor.msgBox.missHardware')
        );
      })
      .finally(() => {
        this.endLoader();
      });
    this.timer = setInterval(() => {
      this.$store.dispatch('performanceMonitor/getHostStatus').catch(() => {
        clearInterval(this.timer);
        this.$bvModal.msgBoxOk(
          this.$t('pagePerformanceMonitor.msgBox.missHardware')
        );
      });
    }, 30000);
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  methods: {
    historyBlockName(hardwareName: string | number) {
      let title = '';
      switch (hardwareName) {
        case 'CPU':
          title = 'CPU';
          break;
        case 'Memory':
          title = this.$t('pagePerformanceMonitor.Memory');
          break;
        case 'Disk':
          title = this.$t('pagePerformanceMonitor.Disk');
          break;
        case 'NetInterface':
          title = this.$t('pagePerformanceMonitor.NetInterface');
          break;
      }
      return title;
    },
  },
};
</script>

<style lang="scss" scoped></style>
